package top.rainbowecho.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.rainbowecho.article.mapper.TagMapper;
import top.rainbowecho.article.service.ITagService;
import top.rainbowecho.article.util.RedisProxy;
import top.rainbowecho.common.util.CacheKey;
import top.rainbowecho.model.percona.Tag;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章的标签 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2020-07-25
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Resource
    private RedisProxy redisProxy;

    @Resource
    private TagMapper tagMapper;

    @Override
    @Transactional
    public List<Tag> getAllTag() {
        return this.redisProxy.getFromCache(CacheKey.TAGS, () -> this.tagMapper.selectList(null));
    }

    @Override
    @Transactional
    public void saveTag(Tag tag) {
        this.tagMapper.insert(tag);
        this.redisProxy.evict(CacheKey.TAGS);
    }
}
